package main

import (
	"ddl_checker/internal/config"
	"ddl_checker/internal/rules"
	"fmt"
	"strings"
)

func main() {
	fmt.Println("=== 简单API检查器演示 ===")

	// 从环境变量获取启用的检查器
	envValidators := config.GetString("ENABLED_VALIDATORS", "")
	var enabledValidators []string

	if envValidators == "" {
		// 默认启用所有检查器
		enabledValidators = rules.GetAllValidatorNames()
	} else {
		enabledValidators = strings.Split(envValidators, ",")
		// 去除空格
		for i, name := range enabledValidators {
			enabledValidators[i] = strings.TrimSpace(name)
		}
	}

	// 创建测试DDL脚本
	ddlScript := `CREATE TABLE test_schema.products (
		id INT PRIMARY KEY,
		name VARCHAR(100) NOT NULL,
		price DECIMAL(10,2),
		description TEXT,
		created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
	);`

	data := rules.DDLCheckData{
		Schema: "test_schema",
		Table:  "products",
		Script: ddlScript,
	}

	fmt.Printf("DDL脚本:\n%s\n", ddlScript)
	fmt.Println("=========================================")
	fmt.Printf("启用的检查器: %v\n", enabledValidators)
	fmt.Println("=========================================")

	// 逐个执行启用的检查器
	for _, validatorName := range enabledValidators {
		fmt.Printf("\n--- 执行 %s ---\n", validatorName)

		// 通过名称创建检查器
		validator := rules.CreateValidatorByName(validatorName)
		if validator == nil {
			fmt.Printf("❌ 检查器不存在: %s\n", validatorName)
			continue
		}

		fmt.Printf("✅ 检查器创建成功: %s\n", validator.Name())
		fmt.Printf("   启用状态: %v\n", validator.IsEnabled())

		// 执行检查
		result := validator.Validate(data)
		fmt.Printf("   检查结果: %s\n", result.Conclusion.String())

		// 显示详细信息
		for _, detail := range result.Details {
			if len(detail) > 0 {
				fmt.Printf("   %s\n", detail)
			}
		}
	}

	fmt.Println("\n=========================================")
	fmt.Println("演示完成！")
}
